package com.yungu.swift.system.marketing.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.marketing.dao.MarketingCouponTemplateMapper;
import com.yungu.swift.system.marketing.model.dto.MarketingCouponTemplateDto;
import com.yungu.swift.system.marketing.model.param.AdminCouponTemplateParam;
import com.yungu.swift.system.marketing.service.MarketingCouponTemplateService;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;


/**
 * @author xiuyin.cui - Generate
 * @version 1.1.0-SNAPSHOT
 * @Modified By:
 * @since 2019-04-25 13:50
 */
@Service
public class MarketingCouponTemplateServiceImpl extends BaseServiceImpl<MarketingCouponTemplateDto> implements MarketingCouponTemplateService {

    @Autowired
    private MarketingCouponTemplateMapper marketingCouponTemplateMapper;

    @Override
    protected IMapper<MarketingCouponTemplateDto> getMapper() {
        return marketingCouponTemplateMapper;
    }

    @Override
    public ResponseData<Boolean> saveOrUpdate(AdminCouponTemplateParam adminCouponTemplateParam) {
        MarketingCouponTemplateDto marketingCouponTemplateDto = new MarketingCouponTemplateDto();
        BeanUtils.copyProperties(adminCouponTemplateParam, marketingCouponTemplateDto);
        if (adminCouponTemplateParam.getMoney() != null) {
            marketingCouponTemplateDto.setMoney(BigDecimal.valueOf(adminCouponTemplateParam.getMoney()));
        }
        if (adminCouponTemplateParam.getAstrict() != null) {
            marketingCouponTemplateDto.setAstrict(BigDecimal.valueOf(adminCouponTemplateParam.getAstrict()));
        }
        if (adminCouponTemplateParam.getDiscount() != null) {
            marketingCouponTemplateDto.setDiscount(BigDecimal.valueOf(adminCouponTemplateParam.getDiscount()));
        }
        if (adminCouponTemplateParam.getHighestMoney() != null) {
            marketingCouponTemplateDto.setHighestMoney(BigDecimal.valueOf(adminCouponTemplateParam.getHighestMoney()));
        }
        int affectedRowCount;
        // uuid不为空则说明是修改 调用修改方法
        if (StringUtils.isNotEmpty(adminCouponTemplateParam.getUuid())) {
            marketingCouponTemplateDto.setUpdateBy(adminCouponTemplateParam.getUserUuid());
            marketingCouponTemplateDto.setUpdateOn(new Date());
            affectedRowCount = marketingCouponTemplateMapper.edit(marketingCouponTemplateDto);
        } else {
            marketingCouponTemplateDto.setUuid(StringUtils.buildUUID());
            marketingCouponTemplateDto.setCreateBy(adminCouponTemplateParam.getUserUuid());
            marketingCouponTemplateDto.setCreateOn(new Date());
            affectedRowCount = marketingCouponTemplateMapper.add(marketingCouponTemplateDto);
        }
        return ResponseData.buildSuccessResponse(affectedRowCount > 0);
    }
}